package com.io.bean.autowired.my;

import org.junit.Test;

import java.lang.reflect.Field;
import java.util.stream.Stream;

/**
 * The class/interface
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class MainTest {
	@Test
	public void test() throws Exception {
		MyController myController = new MyController();
		Class<? extends MyController> clazz = myController.getClass();
		MyService myService = new MyService();
		Field field = clazz.getDeclaredField("myService");
		field.setAccessible(true);
		field.set(myController, myService);
		System.out.println(myController.getMyService());
	}

	@Test
	public void test02() {
		MyService myService = new MyService();
		Class<? extends MyService> clazz = myService.getClass();
		Stream.of(clazz.getDeclaredFields()).forEach(field -> {
			MyAutoWired annotation = field.getAnnotation(MyAutoWired.class);
			if (annotation != null) {
				field.setAccessible(true);
				Class<?> type = field.getType();
				try {
					Object o = type.newInstance();
					field.set(myService, o);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			System.out.println(myService.getMyDao());
		});
	}
}
